<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加申购物品</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form ref="infoForm" :model="infoForm" label-width="120px">
          <el-form-item label="申请类型:" prop="name">
            <el-radio-group v-model="infoForm.type" @change="changeType">
              <el-radio :label="0">物料</el-radio>
              <el-radio :label="1">教具</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item :label="!infoForm.type?'物料名称:':'教具名称:'" prop="name">
                <el-autocomplete
                    class="inline-input"
                    v-model="infoForm.name"
                    size="small"
                    value-key="name"
                    :fetch-suggestions="querySearch"
                    :placeholder="!infoForm.type?'物料名称':'教具名称'"
                    :trigger-on-focus="false"
                    @select="handleSelect"
                ></el-autocomplete>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="!infoForm.type?'物料照片:':'教具照片:'" prop="pic">
                <!--                <div class="pic-line">-->
                <!--                  <el-button size="small" type="primary" @click="actionSheet('add')">选择图片</el-button>-->
                <div class="add-img" @click="actionSheet('add')">
                  <div class="rightapply-box-right-list-box-img-in"
                       :style="{backgroundImage:'url('+infoForm.pic+')',backgroundSize:'cover',backgroundPosition:'center'}">
                    <i class="el-icon-camera-solid" v-if="!infoForm.pic"></i>
                  </div>
                  <!--                  </div>-->
                </div>

              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="申购事由:" prop="reason">
            <el-input v-model="infoForm.reason" placeholder="填写申购事由" size="small"
                      type="textarea"
                      maxlength="100"
                      show-word-limit></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申购数量:" prop="need_number">
                <el-input v-model="infoForm.need_number" placeholder="数量" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单价:" prop="price">
                <el-input v-model="infoForm.price" placeholder="单价" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单位:" prop="need_number" v-if="!infoForm.type">
                <el-input v-model="infoForm.unit" placeholder="单位" size="small"></el-input>
              </el-form-item>
              <el-form-item label="教具类目:" v-if="infoForm.type">
                <el-select placeholder="教具类目" v-model="infoForm.cate_id" size="small" class="w-140" @change="getChange">
                  <el-option :label="item.name" :value="item.id" v-for="(item, index) in cateList"
                             :key="index"></el-option>
                </el-select>
                <el-select placeholder="分类" v-model="infoForm.cate_son_id" size="small" class="w-140"
                           v-if="cateList[infoForm.cate_id] && cateList[infoForm.cate_id].son">
                  <el-option :label="item.name" :value="item.id" v-for="(item, index) in cateList[infoForm.cate_id].son"
                             :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="期望交付时间:" prop="expect_time">
                <el-date-picker type="date"
                                size="small"
                                placeholder="选择日期"
                                v-model="infoForm.expect_time"
                                :picker-options="pickerOptions1"
                                value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="链接/单位:" prop="name">
            <el-input v-model="infoForm.url" placeholder="链接/单位" size="small"></el-input>
          </el-form-item>

          <el-form-item label="补充说明:" prop="remark">
            <el-input v-model="infoForm.remark" placeholder="补充说明" size="small" type="textarea"
                      maxlength="100"
                      show-word-limit></el-input>
          </el-form-item>
          <form style="display: none" enctype="multipart/form-data">
            <input type="file" accept="image/*" @change="handleFile('pic')" class="hiddenInput" id="uploadFile"
                   ref="uploadFile"/>
          </form>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="addGoods">确定并添加</el-button>
      </div>
      <AuxiliaryModule @getInformation="getInformation" :typeData="typeData"></AuxiliaryModule>
    </div>
  </div>
</template>

<script>
export default {
  props: ['visible', 'cateList'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      infoForm: {
        type: 0, // 采购类型0物料1教具
        name: '',
        cate_id: '',
        reason: '',
        unit: '', // "单位"
        need_number: 1,
        price: '',
        pic: '',
        url: '',
        expect_time: '',
        remark: '',
        cate_son_id: ''
      },
      typeData: 'purchase_apply',
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (!this.visible) {
        this.infoForm = {
          type: 0,
          name: '',
          cate_id: '',
          reason: '',
          unit: '', // "单位",
          need_number: 1,
          price: '',
          pic: '',
          url: '',
          expect_time: '',
          remark: '',
          cate_son_id: ''
        }
      }
    }
  },
  created() {

  },
  methods: {
    addGoods() {
      let option = this.getJson(this.infoForm)
      console.log('this.infoForm,', option)
      if (!option.name) {
        this.$message.error(!option.type ? '填写物料名称' : '填写教具名称')
        return
      }
      if (option.type) {
        if (!option.cate_id) {
          this.$message.error('选择教具类目')
          return
        }else{
          if(this.cateList[option.cate_id].son && !option.cate_son_id){
            this.$message.error('选择教具类目')
            return
          }
        }
      }
      if (!option.need_number) {
        this.$message.error('填写申购数量')
        return
      }
      if (!option.type && !option.unit) {
        this.$message.error('填写单位')
        return
      }
      console.log('提交参数：', option)
      this.$emit('addNewGoods', option)
      this.closePOpup()
    },
    changeType() {
      console.log(this.infoForm.type)
      if (!this.infoForm.type) {
        this.infoForm.cate_id = ''
        this.infoForm.cate_son_id = ''
      }else{
        this.infoForm.unit = ''
      }
    },
    querySearch(queryString, cb) {
      console.log('请求服务起：', queryString)
      let that = this;
      // let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
      // 调用 callback 返回建议列表的数据
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        // cb(results);
        if (!this.infoForm.type) {
          that.getmMterial(cb, queryString)
        } else {
          that.getTeaching(cb, queryString)
        }
      }, 3000 * Math.random());
    },
    // 获取物料
    async getmMterial(cb, name) {
      let data = await this.$fetch(this.api.admin_materiel_index, {p: 1, name: name});
      if (!data.code) {
        for (let item of data.list.data) {
          item.testname = item.name
          item.name = item.name + '/' + item.unit
        }
        // this.arryList = data.list.data;
        cb(data.list.data);
      }
    },
    // 获取教具
    async getTeaching(cb, name) {
      let data = await this.$fetch(this.api.tools_index, {p: 1, name: name});
      if (!data.code) {
        // this.arryList = data.list.data;
        cb(data.list.data);
      }
    },
    actionSheet(option, index) {
      this.imgType = option;
      this.picIndex = index;
      if (option !== 'excel') {
        this.$el.querySelector('.hiddenInput').click()
      } else {
        this.$el.querySelector('.hiddenExcelInput').click()
      }
    },
    // 是选中某一列触发的事件,在这里item为选中字段所在的对象
    handleSelect(option) {
      console.log('选中的某项', option);
      if (!this.infoForm.type) {
        this.infoForm.name = option.testname;
        this.infoForm.unit = option.unit;
      } else {
        // 判断分类
        if (option.cate_id) {
          if (this.cateList[option.cate_id]) {
            this.infoForm.cate_id = option.cate_id
          } else {
            for (let item in this.cateList) {
              if (this.cateList[item].son) {
                for (let m of this.cateList[item].son) {
                  if (m.id === option.cate_id) {
                    this.infoForm.cate_id = Number(item);
                    this.infoForm.cate_son_id = m.id;
                  }
                }
              }
            }
          }
        }
      }
    },
    async handleFile(option) {
      // 此处不能使用event.target，否则手机上不能显示
      let n = this;
      let file = document.querySelector('#uploadFile')
      if (option === 'excel') {
        file = document.querySelector('#uploadExcelFile')
      }
      let formData = new FormData()
      if (file.files[0]) {
        // Indicator.open('上传中…')
        formData.append('file', file.files[0])
        // 接下来就可以用 ajax 提交 formData
        // this.handleBeforeUpload(file.files[0])
        let data = await this.$post('upload/oldupload', formData);
        console.log('data:', data)
        if (!data.code) {
          if (this.imgType !== 'excel') {
            if (this.imgType === 'add') {
              n.infoForm.pic = data.data.name
            } else {
              let list = n.getJson(n.purchase[n.picIndex])
              list.pic = data.data.name;
              n.$set(n.purchase, n.picIndex, list)
              // this.imgType = option;
              // this.picIndex = index;
            }
            n.$refs.uploadFile.value = ''
          } else {
            n.excelFile = data.data.name
            n.getExcelDetail()// 获取excel中的数据
            n.$refs.uploadExcelFile.value = ''
          }
        }
      }
    },
    // 同步数据
    getInformation(option) {
      for (let item of option) {
        if (item.type === 'img' && item.number === 1) {
          this.infoForm.pic = item.value[0].url
        }
      }
    },
    getChange() {
      this.cate_son_id = '';
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 700px;
  height: 600px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px 20px 10px 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}

.rightapply-box-right-list-box-img-in {
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f4f4f4;
  margin-right: 10px;
}

.rightapply-box-right-list-box-img-in i {
  font-size: 40px;
  color: #aaa;
}

.add-img .rightapply-box-right-list-box-img-in {
  border: 1px solid #ddd;
  background: #fff;
}

.inline-input {
  width: 100%;
}

.pic-line {
  display: flex;
}
</style>


